const router = require('koa-router')()

//用户管理列表
router.get('/gethistory',async(ctx,next)=>{
    let sql = 'select * from history'
    let res = await ctx.db.query(sql)
    console.log(res)
    ctx.body = {'code':200,'msg':res}
  
  })
  
  //搜索
  router.post('/searchHistory',async(ctx,next)=>{
    let key = ctx.request.body.key
    console.log(key);
    let sql = `select * from history where history like "%${key}%"`
    let res = await ctx.db.query(sql)
    console.log(res);
    ctx.body = {'code':200,'msg':res}
  })

  //更新数据
  router.post('/updatawuliu',async(ctx,next)=>{
    let row = ctx.request.body
    row.edit = false
    console.log(row)
    let sql = 'update feedback set openid=?,solution=?,contact=? where id=?'
    await ctx.db.query(sql,[row.openid,row.solution,row.contact,row.id])

    ctx.body={'code':200,"msg":"编辑成功"}
  
  })
  
  //添加用户数据
  router.post('/addwuliu', async(ctx,next)=>{
    let row = ctx.request.body
    console.log(row);
    let sql = 'insert into feedback(openid,solution,contact) values(?,?,?)'
    await ctx.db.query(sql,[row.openid,row.solution,row.contact])
    
    ctx.body={'code':200,'msg':"添加成功"}
  
  })
  
  
  //删除某个用户数据
  router.post('/deletehistory',async(ctx,next)=>{
    let openid = ctx.request.body.openid
    console.log(openid)
    let sql='delete from history where openid=?'
    await ctx.db.query(sql,[openid])

    ctx.body={"code":200,"msg":'删除成功'}
  })

  module.exports = router